﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using IloveRSS.Core;

namespace IloveRSS
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication
    {
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());
        }

        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                "Default", // Route name
                "", // URL with parameters
                new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
                );

            routes.MapRoute(
                "Account", // Route name
                "Account/{action}/{id}", // URL with parameters
                new { controller = "Account", action = "LogOn", id = UrlParameter.Optional } // Parameter defaults
                );

            routes.MapRoute(
             "Home", // Route name
             "Home/{action}/{id}", // URL with parameters
             new { controller = "Home", action = "Index", id = UrlParameter.Optional }, // Parameter defaults
             new { authenticated = new AuthenticatedConstraint() }
             );

            routes.MapRoute(
                "Settigs", // Route name
                "Settings/{action}/{id}", // URL with parameters
                new { controller = "Settings", action = "Index", id = UrlParameter.Optional }, // Parameter defaults
                new { authenticated = new AuthenticatedConstraint() }
                );

            routes.MapRoute(
                "Search", // Route name
                "Search/{action}/{id}", // URL with parameters
                new { controller = "Search", action = "Index", id = UrlParameter.Optional }, // Parameter defaults
                new { authenticated = new AuthenticatedConstraint() }
                );

            routes.MapRoute(
                "Admin", "Administration/{action}",
                new { controller = "Administration", action = "Index" },
                new { authentiacated = new AdminConstraint() }
                );

        }

        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);

            ControllerBuilder.Current.SetControllerFactory(new ILoveRssControllerFactory());
        }
    }

    public class AdminConstraint : IRouteConstraint
    {
        public bool Match(HttpContextBase httpContext, Route route,
            string parameterName, RouteValueDictionary values,
            RouteDirection routeDirection)
        {
            return httpContext.User.IsInRole("Admin");
        }
    }

    public class AuthenticatedConstraint : IRouteConstraint
    {
        public bool Match(HttpContextBase httpContext, Route route,
            string parameterName, RouteValueDictionary values,
            RouteDirection routeDirection)
        {
            return httpContext.User.Identity.IsAuthenticated;
        }
    }
}